﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace xspf2m3u
{
    class Program
    {

        //Cut "<![CDATA[" and "]]>" to get clean value
        //E.g. "<![CDATA[Channel 1]]>" will be converted to "Channel 1"
        static string CutCDATAtag(string str)
        {
            string tmp = str;
            if (tmp.Contains("CDATA"))
            {
                tmp = tmp.Substring(9); //9 is lenght of "<![CDATA["
                tmp = tmp.Substring(0, tmp.Length - 3); //3 is lenght of "]]>"
            }
            return tmp;
        }


        //convert each 
        //   <location><![CDATA[playlist item]]></location>
        //   <title><![CDATA[item title]]></title>
        //in xpsf playlist to
        //   #EXTINF:-1,item title TV
        //   playlist item  
        //in m3u playlist
        static void Convert(string xpsf)
        {
            string title = "";
            string location = "";
            bool locationCaptured = false;
            bool titleCaptured = false;

            StreamReader istream = new StreamReader(xpsf);
            StreamWriter ostream = new StreamWriter(Path.GetFileNameWithoutExtension(xpsf) + ".m3u");

            ostream.WriteLine("#EXTM3U");

            XmlReader xml = XmlReader.Create(istream);
            
            while (xml.Read())
            {
                switch (xml.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        switch (xml.Name)
                        {
                            case "location":
                                location = CutCDATAtag(xml.ReadInnerXml());
                                locationCaptured = true;
                                break;
                            case "title":
                                title = CutCDATAtag(xml.ReadInnerXml());
                                titleCaptured = true;
                                break;
                        }
                        break;
                 }

                 if (locationCaptured && titleCaptured)
                 {
                     ostream.WriteLine("#EXTINF:-1," + title + " TV");
                     ostream.WriteLine(location);
                     locationCaptured = false;
                     titleCaptured = false;
                 }

            }//while (xml.Read())

            ostream.Close();

        }//static void Convert(string xpsf)

        static void PrintUsage()
        {
            Console.WriteLine("=============================================================================");
            Console.WriteLine("xpsf2m3u v 1.0 (xspf to mp3u converter)");
            Console.WriteLine("This tool converts xpsf playlist to m3u playlist");
            Console.WriteLine();
            Console.WriteLine("Usage: xpsf2m3u <xpsf list>");
            Console.WriteLine("       where <xpsf list> is a file containing playlist in xpsf format");
            Console.WriteLine("Result will be stored in xpsf2m3u working folder with the same name ");
            Console.WriteLine("as xpsf playlist but with m3u extension.");
            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.WriteLine("=============================================================================");
            Console.ReadKey();
        }


        static void Main(string[] args)
        {
            if(args.Length<1)
            {
                PrintUsage();
                return;
            }

            if (File.Exists(args[0]))
            {
                Convert(args[0]);
            }
            else
            {
                PrintUsage();
            }
        }
    }//class Program
}
